﻿using System;
using System.Collections.Specialized;
using System.Data;
using System.IO;
using System.Text;
using SVTTranslateTextEngine.Core.Entries;

namespace SVTTranslateTextEngine.Core.Log
{
    /// <summary>
    /// This class is handle the log how to read
    /// </summary>
    public class LogReader
    {
        protected string m_BaseDirPath;

        protected string[] m_AllLogFilePaths;

        public LogReader(string szBaseDirPath)
        {
            m_BaseDirPath = szBaseDirPath;
            InitLogFilePaths();
        }

        public string[] GetAllLogFilePaths()
        {
            return m_AllLogFilePaths;
        }

        /// <summary>
        /// Init the log file's path which in ten BaseDirPath.
        /// </summary>
        public void InitLogFilePaths()
        {
            m_AllLogFilePaths = Directory.Exists(m_BaseDirPath) ? Directory.GetFiles(m_BaseDirPath, "*.csv", SearchOption.AllDirectories) : null;
        }

        protected void AddLogRecordToDataTable(ref DataTable logData, string szRecord)
        {
            if(!string.IsNullOrEmpty(szRecord))
            {
                var row = logData.NewRow();
                var ss = szRecord.Split(LogInfo.SPLIT_POINT);
                if(ss.Length == logData.Columns.Count)
                {
                    for(var i=0;i<ss.Length;i++)
                    {
                        var colVal = ss[i];
                        var col = logData.Columns[i];
                        if (!string.IsNullOrEmpty(colVal) && col.DataType == typeof(DateTime))
                            row[i] = Convert.ToDateTime(colVal);
                        else
                            row[i] = colVal;
                    }
                }
            }
        }

        protected void FillTheLogRecordsByFilePath(string szFilePath, ref DataTable logData)
        {
            if(File.Exists(szFilePath))
            {
                StreamReader sr=null;
                try
                {
                    sr = new StreamReader(szFilePath, Encoding.UTF8);
                    while(sr.Peek() > 0)
                    {
                        AddLogRecordToDataTable(ref logData, sr.ReadLine());
                    }
                    
                }
                finally
                {
                    if (sr != null)
                    {
                        sr.Dispose();
                        sr.Close();
                    }
                }
            }
        }

        protected DataTable GetLogRecordsByPath(string szPath)
        {
            var resultData = LogInfo.GetDataTableStructure();
            foreach(var filePath in m_AllLogFilePaths)
            {
                if(filePath.StartsWith(szPath))
                {
                    FillTheLogRecordsByFilePath(filePath, ref resultData);
                }
            }
            return resultData;
        }

        /// <summary>
        /// Get the LogRecords by the string of date
        /// </summary>
        /// <param name="szDateString">string of date, format as 'yyyyMMdd'</param>
        /// <returns></returns>
        public DataTable GetLogRecords(string szDateString)
        {
            return GetLogRecordsByPath(m_BaseDirPath+"\\"+szDateString);
        }

        /// <summary>
        /// Get the LogRecords by the string of date and the IP address of request
        /// </summary>
        /// <param name="szDateString"></param>
        /// <param name="szRequestIpAddress"></param>
        /// <returns></returns>
        public DataTable GetLogRecords(string szDateString, string szRequestIpAddress)
        {
            return GetLogRecordsByPath(m_BaseDirPath + "\\" + szDateString + "\\" +szRequestIpAddress);
        }

        /// <summary>
        /// Get the LogRecords by the string of date and the IP address of request and hour value.
        /// szDateString >> szRequestIpAddress >> szHourValue
        /// eg : 20120101 >> 10.169.2.5 >> 15
        /// </summary>
        /// <param name="szDateString"></param>
        /// <param name="szRequestIpAddress"></param>
        /// <param name="szHourValue"></param>
        /// <returns></returns>
        public DataTable GetLogRecords(string szDateString, string szRequestIpAddress, string szHourValue)
        {
            return GetLogRecordsByPath(m_BaseDirPath + "\\" + szDateString +"\\" + szRequestIpAddress +"\\" + szHourValue);
        }
    }
}